//
//  RootViewController.m
//  Accounting
//
//  Created by Billy on 15/8/26.
//  Copyright (c) 2015年 hzqs. All rights reserved.
//

#import "RootViewController.h"
#import "PublishViewController.h"
#import "Util.h"
#import "PaymentViewController.h"

//#define  HOST @"http://www.grep.pub//"

//#define  HOST @"http://192.168.1.116/Home/Index/index/"
#define  HOST @"http://192.168.0.111/Home/Index/index/"

@interface RootViewController ()<UIWebViewDelegate>


@property (nonatomic,strong) UIWebView *webView; //加载整个H5

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self pageSettingsAndSubviewsCreation];
}


- (void) viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}


#pragma mark - SubviewsCreationAndPageSettings

- (void) pageSettingsAndSubviewsCreation {
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 8, self.view.frame.size.width, self.view.frame.size.height-8)];
    [self.webView setDelegate:self];
    [self.view addSubview:self.webView];
    self.view.backgroundColor = UIColorWithRGBA(62, 151, 255, 1);
    NSURL *url = [NSURL URLWithString:HOST];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];

}


#pragma mark - webview delegate


- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSString *url = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    
    
    

    if ([url rangeOfString:@"/Home/Index/index"].location != NSNotFound) {
        /** 得到sessionId 用来做原生的支付和发布问题*/
        NSString *sessionId = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('sessionId').innerHTML"];
        
        NSString *IP = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('IP').innerHTML"];

        if (![IP isEqualToString:@""]) {
            
            [[NSUserDefaults standardUserDefaults] setObject:IP forKey:@"IP"];
            NSLog(@"得到IP 为:%@",IP);
            
        }

        
        if (![sessionId isEqualToString:@""]) {
            
            [[NSUserDefaults standardUserDefaults] setObject:sessionId forKey:@"sessionId"];
            NSLog(@"得到sessionId 为:%@",sessionId);
        }

    }else  if ([url rangeOfString:@"/User/next"].location != NSNotFound) { //发布的监控
        NSLog(@"11111");
        PublishViewController *publishVC = [[PublishViewController alloc] init];
        
        [self.navigationController pushViewController:publishVC animated:YES];
        
    }else if ([url rangeOfString:@"/Alipay"].location != NSNotFound) { //支付监控
        
        NSLog(@"转到支付页");
        
        [self.navigationController pushViewController:[PaymentViewController new] animated:YES];
       // 转到支付页面 PaymentViewController.h
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
